package com.ckf.login_wx.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckf.login_wx.entity.Odd;
import com.ckf.login_wx.entity.Shopper;
import com.ckf.login_wx.entity.User;
import com.ckf.login_wx.mapper.UserMapper;
import com.ckf.login_wx.servic.UserService;
import com.ckf.login_wx.servic.ShopperService;
import com.ckf.login_wx.util.Getpo;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/Sp")
public class SpUserController {

	@Autowired
    private UserService userService;
	@Autowired
    private ShopperService ShopperService;
    @Autowired
    private UserMapper UserMapper;
   
    @PostMapping("/adduser")
    public Object adduser(int id,String username,String password,String truename,String phone,String quxianname,String faf){
        if(userService.list(new QueryWrapper<User>().eq("username", username)).size()!=0&&faf.equals("add")){
        	return "用户名已存在";
        }
    	String stu="";
        User user=new User();
        user.setusername(username);
        user.setPassword(password);
        user.setTruename(truename);
        user.setPhone(phone);
        user.setQuxianname(quxianname);
        String quxian="";
        if(quxianname.equals("生产人员")){
        	quxian="0";
        }else if(quxianname.equals("企业管理员")){
        	quxian="3";
        }else if(quxianname.equals("销售人员")){
        	quxian="2";	
        }else if(quxianname.equals("客户")){
        	quxian="5";	
        }else{
        	quxian="1";
        }
        user.setQuxian(quxian);
		if(faf.equals("add")){
	        if (userService.save(user)){
	        	stu="添加成功";
	        }else {
	        	stu="添加失败";
	        }
		}
		if(faf.equals("up")){
			user.setId(id);
	        if (userService.updateById(user)){
	        	stu="修改成功";
	        }else {
	        	stu="修改失败";
	        }
		}
	    return stu;
    }
    @GetMapping("/userlistall")
    public Object userlistall(HttpServletRequest request){
    	String pageNo=request.getParameter("page");
		String pagesize=request.getParameter("limit");
    	Integer i = Integer.parseInt(pageNo); 
    	Integer n = Integer.parseInt(pagesize); 
        Map<String, Object> map = new HashMap<>();
    	List<User> a= userService.list();
    	map.put("code",0);
        map.put("count",a.size());
        map.put("msg"," ");
    	if(n!=5000)PageHelper.offsetPage((i-1)*n, n);
        map.put("data",userService.list());
    return map;
    }
    @PostMapping("/uppassword")
    public Object uppassword(String oldpwd,String newpwd,String name){
    	Map<String, Object> map = new HashMap<>();
    	QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Map<String, Object> mapuser = new HashMap<>();
        mapuser.put("UserName", name);
        mapuser.put("Password",oldpwd);
        queryWrapper.allEq(mapuser);
        User a=userService.getOne(queryWrapper);
        if (a!=null||oldpwd.equals("修改用户密码")){
        	
        	mapuser.clear();
//        	QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
//            mapuser.put("UserName", name);
//            mapuser.put("Password",newpwd);
//            queryWrapper2.allEq(mapuser);
            User u=new User();
            u.setusername(name);
            u.setPassword(newpwd);
        	if(userService.updateById(u)){
                System.out.println("密码修改成功");
                map.put("result","密码修改成功");
                map.put("code",200);
        	}
        }else {
        	System.out.println(name+"****"+oldpwd);
            map.put("result","密码错误");
            map.put("code",200);
        }
        return map;
    }

    @PostMapping("/seleuserbyid")
    public Object seleuserbyid(int id){
        return userService.getById(id);
    }
    @PostMapping("/upuserbyid")
    public Object upuserbyid(int id,String username,String password,String truename,String phone,String quxianname){
    	Map<String, Object> map = new HashMap<>();
        User user=new User();
        user.setId(id);
        user.setusername(username);
        user.setPassword(password);
        user.setTruename(truename);
        user.setPhone(phone);
        user.setQuxianname(quxianname);
        String quxian="";
        if(quxianname.equals("生产人员")){
        	quxian="0";
        }else if(quxianname.equals("企业管理员")){
        	quxian="3";
        }else if(quxianname.equals("销售员")){
        	quxian="2";	
        }else{
        	quxian="1";
        }
        user.setQuxian(quxian);
		if (userService.updateById(user)){
            map.put("code",200);
            map.put("result","修改成功");
            System.out.println("修改成功");
        }else {
        	map.put("code",200);
            map.put("result","no");
        }
        return map;
    }
    @RequestMapping("/seleusernameall")
    public Object seleusernameall(){
    	userService.list();
    	List<String> s =new ArrayList<String>();
    	for(int i=0;i<userService.list().size();i++){
    		if(userService.list().get(i).getQuxian().equals("2")||userService.list().get(i).getQuxian().equals("3"))
    		s.add(userService.list().get(i).getUsername());
    	}
    	
        return s;
    }
    @RequestMapping("/selenameall")
    public Object selenameall(){
    	userService.list();
    	List<String> s =new ArrayList<String>();
    	for(int i=0;i<userService.list().size();i++){
    		if(userService.list().get(i).getQuxian().equals("0"))
    		s.add(userService.list().get(i).getUsername());
    	}
    	
        return s;
    }
    //查询所有销售人员
    @RequestMapping("/seleallanq")
    public Object seleallanq(HttpServletRequest request){
    	HttpSession session= request.getSession();
        String quxian = session.getAttribute("quxian")+"";
        String truename = session.getAttribute("truename")+"";
    	Map<String, Object> map = new HashMap<>();
    	List<User> user =userService.list();
    	List<String> s =new ArrayList<String>();
        if(quxian.equals("2")){
    		s.add(truename);
            map.put("count",s.size());
            map.put("data",s);
            return map;
        }
    	for(int i=0;i<user.size();i++){
    		if(user.get(i).getQuxian().equals("2"))
    		s.add(user.get(i).getTruename());
    	}
        map.put("count",s.size());
        map.put("data",s);
        return map;
    }
    //查询所有生产人员
    @RequestMapping("/seleallsaisie")
    public Object seleallsaisie(){
    	List<User> user =userService.list();
    	List<String> s =new ArrayList<String>();
    	for(int i=0;i<user.size();i++){
    		if(user.get(i).getQuxian().equals("0"))
    		s.add(user.get(i).getTruename());
    	}
    	Map<String, Object> map = new HashMap<>();
        map.put("count",s.size());
        map.put("data",s);
        return map;
    }
    //权限查询所有生产人员
    @RequestMapping("/seleallsaisie2")
    public Object seleallsaisie2(HttpServletRequest request){
    	HttpSession session= request.getSession();
        String username = session.getAttribute("username")+"";
        String quxian = session.getAttribute("quxian")+"";
        String truename = session.getAttribute("truename")+"";
        
    	List<User> user =userService.list();
    	List<String> s =new ArrayList<String>();
    	if(quxian.equals("4")||quxian.equals("1")){
	    	for(int i=0;i<user.size();i++){
	    		if(user.get(i).getQuxian().equals("0"))
	    		s.add(user.get(i).getTruename());
	    	}
    	}else{
    		s.add(truename);
    	}
    	Map<String, Object> map = new HashMap<>();
        map.put("count",s.size());
        map.put("data",s);
        return map;
    }
    //查询所有制单员
    @RequestMapping("/seleallzhidan")
    public Object seleallzhidan(HttpServletRequest request){
    	HttpSession session= request.getSession();
        String quxian = session.getAttribute("quxian")+"";
        String truename = session.getAttribute("truename")+"";
    	List<User> user =userService.list();
    	List<String> s =new ArrayList<String>();

    	if(quxian.equals("4")){
	    	for(int i=0;i<user.size();i++){
	    		if(user.get(i).getQuxian().equals("1"))
	    		s.add(user.get(i).getTruename());
	    	}
    	}else{
    		s.add(truename);
    	}
    	Map<String, Object> map = new HashMap<>();
        map.put("count",s.size());
        map.put("data",s);
        return map;
    }
    //查询所有客户
    @RequestMapping("/seleallshopper")
    public Object seleallshopper(HttpServletRequest request){
    	HttpSession session= request.getSession();
        String quxian = session.getAttribute("quxian")+"";
//    	if(quxian.equals("2")){
//    		return "";
//	    }
    	List<Shopper> Shopper =ShopperService.list();
    	List<String> name =new ArrayList<String>();
    	for(int i=0;i<Shopper.size();i++){
    		name.add(Shopper.get(i).getName());
    	}
    	Map<String, Object> map = new HashMap<>();
        map.put("name",name);
        map.put("count",Shopper.size());
        map.put("data",Shopper);
        return map;
    }
    @GetMapping("/deleuserbyid")
	public Object deleuserbyid(int id){
		Map<String, Object> map = new HashMap<>();
		if(userService.getById(id).getUsername().equals("admin")){
	        map.put("result","超管账号无法删除");
	    	return map;
		}
	    if (userService.removeById(id)){
	        map.put("result","删除成功");
	    }else {
	        map.put("result","删除失败");
	    }
	return map;
	}
	@RequestMapping("/deleuserall")
    public Object deleuserall(String[] id){
    	
    	for (int i = 0; i < id.length; i++) {
    		try {
    			if(id[i].equals("")||userService.getById(id).getUsername().equals("admin"))continue;

    	    	userService.removeById(id[i]);
			} catch (Exception e) {
				System.out.println("***********************************异常*************************************");
				return "异常";
			}
		}
		return "删除成功";
    }
    @RequestMapping("/seleusersearch")
    public Object seleusersearch(HttpServletRequest request,String username,String truename){
    	String pageNo=request.getParameter("page");
		String pagesize=request.getParameter("limit");
    	Integer i = Integer.parseInt(pageNo); 
    	Integer n = Integer.parseInt(pagesize); 
    	
//    	QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        Map<String, Object> mapuser = new HashMap<>();
//        if(username!=null&&!username.equals(""))mapuser.put("username",username);
//        if(truename!=null&&!truename.equals(""))mapuser.put("truename",truename);
//        queryWrapper.allEq(mapuser);
    	
    	Map<String, Object> map = new HashMap<>();
    	List<User> a= userService.list(new QueryWrapper<User>().like("username",username).like("truename",truename));
    	map.put("code",0);
        map.put("count",a.size());
        map.put("msg"," ");
    	if(n!=5000)PageHelper.offsetPage((i-1)*n, n);
        map.put("data",userService.list(new QueryWrapper<User>().like("username",username).like("truename",truename)));
    	return map;
    }
    @PostMapping("/addshopper")
    public Object addshopper(Integer id,String name,String phone,String address,String cid,String faf){
    	String stu="";
        Shopper shopper=new Shopper();
        shopper.setName(name);
        shopper.setPhone(phone);
        shopper.setAddress(address);
        shopper.setCid(cid);
		if(faf.equals("add")){
	        if (ShopperService.save(shopper)){
	        	stu="添加成功";
	        }else {
	        	stu="添加失败";
	        }
		}
		if(faf.equals("up")){
			shopper.setId(id);
	        if (ShopperService.updateById(shopper)){
	        	stu="修改成功";
	        }else {
	        	stu="修改失败";
	        }
		}
	    return stu;
    }
    @GetMapping("/seleshopper")
    public Object seleshopper(HttpServletRequest request){
    	String pageNo=request.getParameter("page");
		String pagesize=request.getParameter("limit");
    	Integer i = Integer.parseInt(pageNo); 
    	Integer n = Integer.parseInt(pagesize); 
        Map<String, Object> map = new HashMap<>();
    	List<Shopper> a= ShopperService.list();
    	map.put("code",0);
        map.put("count",a.size());
        map.put("msg"," ");
    	if(n!=5000)PageHelper.offsetPage((i-1)*n, n);
        map.put("data",ShopperService.list());
    return map;
    }
    @PostMapping("/seleshopperbyid")
    public Object seleshopperbyid(Integer id){
        return ShopperService.getById(id);
    }
    @PostMapping("/seleshopperbyphone")
    public Object seleshopperbyphone(String phone){
    	Map<String, Object> map = new HashMap<>();
    	List<Shopper> s=ShopperService.list(new QueryWrapper<Shopper>().eq("phone",phone));
        map.put("count",s.size());
        map.put("data",s);
    	return map;
    }
    @PostMapping("/seleshopperbyname")
    public Object seleshopperbyname(String name){
        return ShopperService.list(new QueryWrapper<Shopper>().eq("name", name));
    }
    @RequestMapping("/deleshopperall")
    public Object deleshopperall(String[] id){
    	
    	for (int i = 0; i < id.length; i++) {
    		try {
    			if(id[i].equals(""))continue;
    			ShopperService.removeById(id[i]);
			} catch (Exception e) {
				System.out.println("***********************************异常*************************************");
				return "异常";
			}
		}
		return "删除成功";
    }
    @GetMapping("/deleshopperbyid")
	public Object deleshopperbyid(Integer id){
		Map<String, Object> map = new HashMap<>();
	    if (ShopperService.removeById(id)){
	        map.put("result","删除成功");
	    }else {
	        map.put("result","删除失败");
	    }
	return map;
	}
    @RequestMapping("/seleshoppersearch")
    public Object seleshoppersearch(HttpServletRequest request,String name,String phone,String address){
    	String pageNo=request.getParameter("page");
		String pagesize=request.getParameter("limit");
    	Integer i = Integer.parseInt(pageNo); 
    	Integer n = Integer.parseInt(pagesize); 
    	
    	Map<String, Object> map = new HashMap<>();
    	List<Shopper> a= ShopperService.list(new QueryWrapper<Shopper>().like("name",name).like("phone",phone).like("address",address));
    	map.put("code",0);
        map.put("count",a.size());
        map.put("msg"," ");
    	if(n!=5000)PageHelper.offsetPage((i-1)*n, n);
        map.put("data",ShopperService.list(new QueryWrapper<Shopper>().like("name",name).like("phone",phone).like("address",address)));
    	return map;
    }
    @RequestMapping("/selephone")
    public boolean selephone(String phone){
		if(ShopperService.list(new QueryWrapper<Shopper>().eq("phone", phone)).size()==0){
			return true;
		}else
		return false;
	}
    @RequestMapping("/jian")
    public void jian(String username){
    	String he="";

    	
    	
    	
    	
    	String s[]={
    			"	BINDID	NUMBER(9)	 COMMENT'	主键	'	, ",
    			"	MOUSERID	NUMBER(9)	 COMMENT'	关联移动用户	'	, ",
    			"	BINDTIME	DATE	 COMMENT'	绑定设备时间	'	, ",
    			"	DEVICEID	VARCHAR2(4000)	 COMMENT'	设备型号	'	, ",
    			"	VALID	NUMBER(1)	 COMMENT'	是否有效	(	0无效,1有效	)'	, ",
    			"	PHONE	VARCHAR2(50)	 COMMENT'	手机号'	, ",
    			"	TOKEN	VARCHAR2(4000)	 COMMENT'	令牌	'	, ",
    			"	USERID	NUMBER(9)	 COMMENT'	PC用户ID	'	 "


    			};

//殡葬单位BSP对接表MOBIND
    	for (int i = 0; i < s.length; i++) {
			he+=s[i];
		}
    	he=he.replace("VARCHAR2", "VARCHAR");
    	he=he.replace("varchar2", "varchar");
    	he=he.replace("NUMBER", "bigint");
    	he=he.replace("number", "bigint");
    	he=he.replace("clob", "text");
    	he=he.replace("CLOB", "text");
//    	he=he.replace("NUMBER(", "decimal(");
//    	he=he.replace("number(", "decimal(");
    	String tou ="CREATE TABLE "+"MOBIND"+"(";
    	String wei =")COMMENT='"+"设备绑定信息表"+"'";
    	UserMapper.jian(tou+he+wei);
    	//做一个万能储存数据表，自由登记选项ABCD,登记记录表。解决繁杂数据录入（搭配exc使用）
    }
}
